﻿package engine.display
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import newx.com.control.scroll.text.NxTalkTextFiled;
	
	public class TalkBox extends Sprite
	{
		public  var isUse:Boolean;

		//背景参数
		private  var bgColor			:uint			=	0x000000;	//背景颜色
		private  var borderColor		:uint			=	0xFF9900;	//线条颜色
		private  var textColor			:uint			=	0xffff99;	//0xf9f9f9;	//字体颜色
		private  var bgAlpha			:Number			=	0.7;		//背景透明度
		private var rate				:int			=	2;
		private var time				:Timer;
		private var showTime			:uint			=	4000;
		private var textMaxHeight		:int			=	300;
		private var textMaxLen			:int			=	100;
		private var textMaxWidth		:uint			=	200;
		private var backSpace			:Sprite;
		
		private var faceArray			:Array;
		private var _height:Number;
		private var _widht:Number
		private var textLabel			:NxTalkTextFiled;
		
		public function TalkBox()
		{
			backSpace				=	new Sprite();
			backSpace.x	=	backSpace.y	=	-3;
			addChild(backSpace);
			
			textLabel				=	new NxTalkTextFiled(textMaxWidth);
			textLabel.textColor		=	textColor;
			addChild(textLabel);
			
			time	=	new Timer(showTime,	1);
			time.addEventListener(TimerEvent.TIMER_COMPLETE,	timerHandler);
			
			this.mouseChildren	=	this.mouseEnabled	=	false;
		}
		
		public function setTalk(content : String):void
		{
			isUse = false;
			textLabel.insterTalkStr(content)
			drawBack();
			starTimer();
		}
		/**
		 *删除对象 
		 * 
		 */		
		public function removeTalkBox():void
		{
			time.stop();
			textLabel.clearAll();
			this.parent.removeChild(this);
			isUse = true;
		}
		private function drawBack():void
		{
			backSpace.graphics.clear();
			backSpace.graphics.lineStyle(1,borderColor);
			backSpace.graphics.beginFill(bgColor,.7);
			backSpace.graphics.drawRect(0,0,textLabel.showTextWidth + 12, textLabel.showTextHeight + 10);
			backSpace.graphics.endFill();
		}
		private function starTimer():void
		{
			time.reset();
			time.start();
		}
		
		private function timerHandler(e:TimerEvent):void
		{
			if(this != null && this.parent != null)
			{
				removeTalkBox();
			}
		}

		override public function get height():Number
		{
			return backSpace.height
		}

		override public function get width():Number
		{
			return backSpace.width;
		}


	}
}